﻿namespace ThreeByte.ActiveDeck.Dashboard
{
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using ThreeByte.ActiveDeck.Web;
    using System.ServiceModel.DomainServices.Client;
    using System;
    using ThreeByte.ActiveDeck.Dashboard.Controls;

    /// <summary>
    /// Home page for the application.
    /// </summary>
    public partial class Downloads : Page
    {
        /// <summary>
        /// Creates a new <see cref="Downloads"/> instance.
        /// </summary>
        public Downloads() {
            InitializeComponent();

        }

        private static readonly string DEVELOPER_ROLE = @"Developer";
        private static readonly string GURU_ROLE = @"Guru";
        
        /// <summary>
        /// Executes when the user navigates to this page.
        /// </summary>
        protected override void OnNavigatedTo(NavigationEventArgs e) {
            BetaDownloadPanel.Visibility = (WebContext.Current.User.IsInRole(DEVELOPER_ROLE) ? Visibility.Visible : Visibility.Collapsed);
            PromoteButton.Visibility = (WebContext.Current.User.IsInRole(GURU_ROLE) ? Visibility.Visible : Visibility.Collapsed); ;
        }

        private DashboardAccessContext _dashboardContext;

        private void Download_Click(object sender, System.Windows.RoutedEventArgs e) {
            //Record a download request
            _dashboardContext = new DashboardAccessContext();
            _dashboardContext.GetDownloadLink(DownloadLink_Callback, null);
        }

        private void DownloadLink_Callback(InvokeOperation<string> op) {
            if(!(op.HasError)) {
                //Navigate a new frame to that link
                System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(op.Value, UriKind.Absolute), "_blank");
            }
        }

        private void DashboardDataContext_LoadedData(object sender, LoadedDataEventArgs e) {
            if(!(e.HasError)) {
                DownloadButton.IsEnabled = true;
            }
        }

        private void DownloadBeta_Click(object sender, System.Windows.RoutedEventArgs e) {
            string version = AvailableBetaVersion.Text;
            string betaLink = string.Format("https://svc.activedeck.com/Config.svc/DownloadBeta?v={0}", version);
            System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(betaLink, UriKind.Absolute), "_blank");
        }

        private void BetaDownloadDataContext_LoadedData(object sender, LoadedDataEventArgs e) {
            if(!(e.HasError)) {
                DownloadBetaButton.IsEnabled = true;
            }
        }

        private void PromoteButton_Click(object sender, RoutedEventArgs e) {
            //Show a confirmation dialog

            ConfirmDialog newConfirm = new ConfirmDialog("Are you sure you want to publish this version to production?", "Move to Production");
            newConfirm.Closed += new EventHandler(newConfirm_Closed);
            newConfirm.Show();
        }

        private DashboardAccessContext ctx;
        void newConfirm_Closed(object sender, EventArgs e) {
            ConfirmDialog dialog = (ConfirmDialog)sender;
            if(dialog.DialogResult ?? false){
                //Make the change
                ctx = new DashboardAccessContext();
                ctx.PromoteBetaDownloadVersion(Promote_Complete, null);
            }
        }

        void Promote_Complete(InvokeOperation<bool> op) {
            if(op.HasError) {
                //Report this;
                SetMessage("Error connecting to webservice.", true);
            }

            if(op.Value) {
                SetMessage("New version published successfully.");
            } else {
                SetMessage("Error publishing to production.", true);
            }
        }

        private void SetMessage(string message = null, bool error = false) {
            if(error) {
                MessageText.Text = null;
                ErrorText.Text = message;
            } else {
                MessageText.Text = message;
                ErrorText.Text = null;
            }
        }
    }
}